package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.ExamPO;
import com.ajmd.onlineexam.entity.PersonExamPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.exception.AppException;
import com.ajmd.onlineexam.mapper.ExamMapper;
import com.ajmd.onlineexam.service.ExamService;
import com.ajmd.onlineexam.vo.ExamVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.ObjectUtils;
import org.omg.CORBA.Object;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ajmd
 * @since 2023/5/3 16:13
 */
@RestController
@RequestMapping("/exam")
public class ExamController {
    @Resource
    private ExamService examService;

    @GetMapping("/select")
    public CommonResult getExamVO(@RequestParam String examId){
        ExamVO examVO = examService.getOneVO(examId);
        if(ObjectUtils.isEmpty(examVO)){
            return CommonResult.error(AppExceptionCodeEnum.EXAM_NOT_EXIST);
        }
        return CommonResult.ok().data("exam",examVO);
    }

    @PostMapping("/addOrUpdate")
    public CommonResult addOrUpdateExam(@MultiRequestBody ExamPO examPO, @MultiRequestBody boolean publishReminder){
        if(ObjectUtils.isEmpty(examPO.getExamName()) || ObjectUtils.isEmpty(examPO.getTestPaperId())
                || ObjectUtils.isEmpty(examPO.getTimeLimit()) || ObjectUtils.isEmpty(examPO.getPassScore())
                || ObjectUtils.isEmpty(examPO.getExaminees())){
            CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        int num = examService.addOrUpdateExam(examPO,publishReminder);
        if(num==0){
            CommonResult.error(AppExceptionCodeEnum.EXAM_PUBLISH_ERR);
        }
        String msg="发布成功！";
        if(ObjectUtils.isEmpty(examPO.getId())){
            msg="编辑成功！";
        }
        return CommonResult.ok().message(msg);
    }


    @PostMapping("/selectPage")
    public CommonResult selectPage(@MultiRequestBody Integer userType,@MultiRequestBody Integer currentPage,
                                   @MultiRequestBody Integer pageSize,@MultiRequestBody SearchParam searchParam){
        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<ExamPO> pageData = examService.getExamPage(currentPage, pageSize, searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }


    @PostMapping("/delete")
    public CommonResult deleteExam(@MultiRequestBody String examId,@MultiRequestBody String account,
                                   @MultiRequestBody Integer userType){
        if(ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(!StaticFinalValue.ADMIN.equals(userType)){
            ExamPO examPO = examService.getOne(examId);
            if(ObjectUtils.isEmpty(examPO)){
                return CommonResult.error(AppExceptionCodeEnum.EXAM_NOT_EXIST);
            }
            //前端已限制，可不加
//            String createPerson = examPO.getCreatePerson();
//            if(ObjectUtils.isNotEmpty(createPerson) && !createPerson.equals(account)){
//                //非管理员且非考试发布人无权删除考试
//                return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
//            }
//            if(StaticFinalValue.IN_PROGRESS.equals(examPO.getStatus())){
//                //1进行中和2已结束的考试不能删除，只有0未开考和3已关闭的才能删除
//                return CommonResult.error().message("考试进行中，无法删除！");
//            }
//            if(StaticFinalValue.ENDED.equals(examPO.getStatus())){
//                return CommonResult.error().message("无法删除！请先关闭考试！");
//            }
        }

        int num = examService.examDelete(examId);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.EXAM_DELETE_ERR);
        }
        return CommonResult.ok().message("考试删除成功");
    }


    @GetMapping("/closeExam")
    public CommonResult closeExam(@RequestParam String examId){
        int num = examService.closeExam(examId);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.EXAM_CLOSE_ERR);
        }
        return CommonResult.ok().message("关闭成功");
    }

    @PostMapping("/remindExam")
    public CommonResult remindExam(@RequestBody ExamPO examPO){
        int num = examService.sendMsg(examPO);
        if(num==0){
            return CommonResult.error().message("消息提醒失败");
        }
        return CommonResult.ok().message("已发送提醒");
    }

    @PostMapping("/student")
    public CommonResult findPage(@MultiRequestBody String account,@MultiRequestBody Integer currentPage,
                               @MultiRequestBody Integer pageSize,@MultiRequestBody SearchParam searchParam){

        IPage<ExamVO> pageData = examService.studentGetPage(account, currentPage, pageSize, searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }

    @PostMapping("/getExamRecords")
    public CommonResult getExamRecords(@MultiRequestBody String account,
                                       @MultiRequestBody String examId,
                                       @MultiRequestBody Integer currentPage,
                                       @MultiRequestBody Integer pageSize){
        Page<PersonExamPO> pageData = examService.getExamRecords(account, examId, currentPage, pageSize);
        return CommonResult.ok().data("pageData",pageData);
    }
}
